import store from "../store";
import Home from "../views/Home.vue";
/** 获取授权信息 */
export function getAuth() {
  /** 全局路由触发这个方法  如果有缓存暂时在这里交与 */
  // if (Lockr.get('Admin-Token') && !axios.defaults.headers['Admin-Token']) {
  //     cache.updateAxiosCache()
  // }

  // if (axios.defaults.headers['Cookie']) {
  //     return true
  // }

  let urlParams = location.search;
  let ticket = getUrlKey("ticket", urlParams);
  if (ticket) {
    sessionStorage.setItem("ticket", ticket);
  }

  ticket = sessionStorage.getItem("ticket");
  if (ticket == null || ticket == "" || ticket == undefined) {
    return false;
  }
  return true;
}
/** 移除权限 */
export function removeAuth() {
  // sessionStorage.clear("ticket")
  store.commit("SET_USERINFO", null);
  store.commit("SET_ALLAUTH", null);
}

/**获取url上的可以值 */
export function getUrlKey(name, url) {
  return (
    decodeURIComponent(
      (new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(url) || [
        ,
        "",
      ])[1].replace(/\+/g, "%20")
    ) || null
  );
}
//获取菜单
export function getMenu() {
  let menuList = [
    { id: 1, parentId: -1, title: "首页", code: "index", path: "/index" },
    {
      id: 2,
      parentId: -1,
      title: "组件管理",
      code: "component",
      path: "/comt",
    },
    {
      id: 3,
      parentId: 1,
      title: "echart图表",
      code: "echart",
      path: "/comt/echart",
    },
    {
      id: 4,
      parentId: 2,
      title: "表单",
      code: "editForm",
      path: "/comt/editForm",
    },
    {
      id: 5,
      parentId: 2,
      title: "公共表格",
      code: "commonTable",
      path: "/comt/commonTable",
    },
  ];
  return menuList;
}

//获取菜单
export function setMenu(data) {
  let menu = {
    path: "/Home",
    component: Home,
    hidden: false,
    children: [
      {
        path: "/",
        redirect: "/workStation", // appChange、workStation
      },
      {
        path: "/userInfo",
        meta: {
          title: "个人信息",
          type: "UserInfo",
        },
        component: () => import("@/views/UserInfo"),
      },
      {
        path: "/entrustManage",
        meta: {
          title: "委托管理",
          type: "UserInfo",
        },
        component: () => import("@/views/entrustManage"),
      },
      {
        path: "/defectOrderAll",
        meta: {
          title: "缺陷工单",
          type: "defectOrderAll",
        },
        component: () => import("@/views/defectOrderAll"),
      },
      {
        path: "/workStation",
        meta: {
          title: "个人工作台",
          type: "workStation",
        },
        component: () => import("@/views/workStation"),
      },
      {
        path: "/templatePage",
        meta: {
          title: "空白页面",
        },
        component: () => import("@/components/templatePage"),
      },
    ],
  };
  let arr = [];
  data.forEach((item, index) => {
    arr.push({
      path: `/${item.menuCode}`,
      meta: {
        title: item.name,
        code: item.menuCode,
        isOpen: item.isOpen,
        isEnable: item.status,
      },
      component: () => import(`@/views${item.menuUrl}/`),
      children: [],
    });
    if (item.children) {
      let list = item.children.find(
        (element) => element.isOpen && element.status
      );
      arr[index].children.push({
        path: "/",
        redirect: list.menuCode,
      });
      item.children.forEach((child, i) => {
        arr[index].children.push({
          path: child.menuCode,
          meta: {
            title: child.name,
            parentCode: item.menuCode,
            code: child.menuCode,
            isOpen: item.isOpen && child.isOpen,
            isEnable: item.status && child.status,
          },
          component: () => import(`@/views${child.menuUrl}/`),
          children: [],
        });
        if (child.children) {
          let list = child.children.find(
            (element) => element.isOpen && element.status
          );
          arr[index].children[i + 1].children.push({
            path: "/",
            redirect: list.menuCode,
          });
          child.children.forEach((second, j) => {
            arr[index].children[i + 1].children.push({
              path: second.menuCode,
              meta: {
                title: second.name,
                parentCode: item.menuCode,
                code: child.menuCode,
                isOpen: item.isOpen && child.isOpen && second.isOpen,
                isEnable: item.status && child.status && second.status,
              },
              component: () =>
                import(`@/views${second.menuUrl}/`),
              children: [],
            });
            if (second.children) {
              let list = second.children.find(
                (element) => element.isOpen && element.status
              );
              arr[index].children[i + 1].children[j + 1].children.push({
                path: "/",
                redirect: list.menuCode,
              });
              second.children.forEach((third, k) => {
                arr[index].children[i + 1].children[j + 1].children.push({
                  path: third.menuCode,
                  name: third.menuCode,
                  meta: {
                    title: third.name,
                    parentCode: item.menuCode,
                    code: child.menuCode,
                    isOpen: item.isOpen && child.isOpen && second.isOpen && third.isOpen,
                    isEnable: item.status && child.status && second.status && third.status,
                  },
                  component: () => import(`@/views${third.menuUrl}/`),
                  children: [],
                });
              });
            } else {
              arr[index].children[i + 1].children[j + 1].name = second.menuCode;
            }
          });
        } else {
          arr[index].children[i + 1].name = child.menuCode;
        }
      });
    }
  });
  menu.children = menu.children.concat(arr);
  return [
    menu,
    {
      path: "*",
      redirect: "/templatePage",
    },
  ];
}
